package algotithm.weekendrace.biweeklycontest47.test1;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/6-22:48
 * @Title 5680. 找到最近的有相同 X 或 Y 坐标的点
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int x = 3;
        int y = 4;
        int[][] points = {{1,2},{3,1},{2,4},{2,3},{4,4}};
        int res = solution.nearestValidPoint(x,y,points);
        System.out.println(res);
    }

    public int nearestValidPoint(int x, int y, int[][] points) {
        int res = -1;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < points.length; i++) {
            int[] p = points[i];
            if (x != p[0] && y != p[1]) {
                continue;
            }
            int distance = Math.abs(x - p[0]) + Math.abs(y - p[1]);
            if (distance < min) {
                min = distance;
                res = i;
            }
        }
        return res;
    }
}

